#!/usr/bin/env node
import { program } from 'commander'
import inquirer from 'inquirer'
import { wifi } from './wifi/index.js'
import { signIn } from './signIn/index.js'
import { createRequire } from 'module';  
import {getGetRegistryInfo} from './d/index.js'
const require = createRequire(import.meta.url);  
const json = require('../package.json');  

import { checkPath, downloadTemp } from './utils.js'

program.version(json.version) //创建版本号
//添加create 命令 和 别名crt 以及描述 以及 执行完成之后的动作
program.command('create <project>').alias('ctr').description('create a new project').action((project) => {
  //命令行交互工具
  inquirer.prompt([
    {
      type: 'input',
      name: 'projectName',
      message: 'project name',
      default: project
    },
    {
      type: 'confirm',
      name: 'isTs',
      message: '是否支持typeScript',
    }
  ]).then((answers) => {
    if (checkPath(answers.projectName)) {
      console.log('文件已存在')
      return
    }

    if (answers.isTs) {
      downloadTemp('ts', answers.projectName)
    } else {
      downloadTemp('js', answers.projectName)
    }
  })
})
program.command('tools').description('使用工具').action(() => {
  inquirer.prompt([{
    type: 'list',
    message: '请选择一种工具:',
    name: 'type',
    choices: [
      "wifi",
      "掘金签到",
      "检测当前npm源"
    ],
    filter: function (val) { // 使用filter将回答变为小写
      return val.toLowerCase();
    }
  }]).then(res => {
    if (res.type === 'wifi') {
      wifi()
    }
    if (res.type === '掘金签到') {
      signIn()
    }
    if(res.type === '检测当前npm源'){
      getGetRegistryInfo()
    }
  })
})
// program.
program.parse(process.argv)
